package activity

import (
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/admin"
	"gq_volunteer_platform/model/organization"
	"gq_volunteer_platform/model/user"
)

func ActivityFindUser(act Activity) (users []user.User, err error) {
	// 查找该活动参与的用户
	var actUser []ActivityUser
	if err := bean.MysqlDb.Where("`activity_id`=?", act.ID).
		Where("`check_status` = '已审核'").
		Find(&actUser).Error; err != nil {

		bean.Log.Warning(err)
		return users, err
	}

	// 查找 user
	var userIds []uint
	for _, item := range actUser {
		userIds = append(userIds, item.UserId)
	}

	if len(userIds) == 0 {
		return users, nil
	}

	if err := bean.MysqlDb.Where("`id` in ?", userIds).
		Find(&users).Error; err != nil {

		bean.Log.Warning(err)
		return users, err
	}

	return users, nil
}

func ActivityFindAdminUser(act Activity) (users []user.User, org organization.Organization, err error) {
	// 查找组织名称
	if err := bean.MysqlDb.Where("`id`=?", act.OrganizationId).
		Find(&org).Error; err != nil {

		bean.Log.Warning(err)
		return users, org, err
	}

	// 查找组织管理员 已绑定
	var adm []admin.Admin
	if err := bean.MysqlDb.Where("`organization_id`=?", act.OrganizationId).
		Where("`user_id` <> 0").
		Find(&adm).Error; err != nil {

		bean.Log.Warning(err)
		return users, org, err
	}

	// 查找管理员绑定的 user
	var userIds []uint
	for _, item := range adm {
		userIds = append(userIds, item.UserId)
	}

	if len(userIds) == 0 {
		return users, org, nil
	}

	if err := bean.MysqlDb.Where("`id` in ?", userIds).
		Find(&users).Error; err != nil {

		bean.Log.Warning(err)
		return users, org, err
	}

	return users, org, nil
}

func ActivityStatus(act Activity) string {
	if act.Status == "招募中" {
		return "已通过"
	}

	return act.Status
}
